package com.marvels.approve.loan.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fintech.third.service.credit.CreditReportService;
import com.marvels.approve.common.dto.LoanContactCheckDTO;
import com.marvels.approve.consum.ApproveConsumerExternalApi;
import com.marvels.approve.credit.service.ApproveThirdCreditReportService;
import com.marvels.approve.loan.dao.IMcamLoanApplyInfoDao;
import com.marvels.approve.loan.dao.McamAutoDecisionMapper;
import com.marvels.approve.loan.dao.McamAutoDecisionRecordMapper;
import com.marvels.approve.loan.dao.McamLoanApplyContactInfoMapper;
import com.marvels.approve.loan.dto.McamAutoDecisionRecord;
import com.marvels.approve.loan.dto.McamAutoDecisionWithBLOBs;
import com.marvels.approve.loan.dto.McamLoanApplyContactInfo;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.loan.dto.RiskTipsDataDto;
import com.marvels.approve.loan.dto.RiskTipsDataDto.RiskTipsDataItem;
import com.marvels.approve.loan.dto.ZxDataReqDTO;
import com.marvels.approve.loan.dto.ZxDataRespDTO;
import com.marvels.approve.loan.service.LoanRiskTipsService;
import com.marvels.approve.process.dao.TaskProcessDao;
import com.marvels.common.exception.MarvelsBusinessException;
@Service
public class LoanRiskTipsServiceImpl implements LoanRiskTipsService {
	@Autowired
	private IMcamLoanApplyInfoDao loanApplyInfoDao;
	@Autowired
	private McamLoanApplyContactInfoMapper loanApplyContactInfoMapper;

	@Autowired
	private TaskProcessDao taskProcessDao;
	@Autowired
	private ApproveConsumerExternalApi consumerExternalApi;
	
	@Autowired
	private McamAutoDecisionMapper mcamAutoDecisionMapper;
	
	@Autowired
	private McamAutoDecisionRecordMapper mcamAutoDecisionRecordMapper;
	
	@Autowired
    private CreditReportService creditReportService;
    
    @Autowired
    private ApproveThirdCreditReportService thirdCreditReportService;
	
	
	
	@Override
	public RiskTipsDataDto queryMxData(String loanId) throws MarvelsBusinessException {
		//魔蝎数据提示
		RiskTipsDataDto riskTip = new RiskTipsDataDto();
		//查本地流水
		McamLoanApplyInfoDTO loanInfo = loanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
		ZxDataReqDTO reqDto = new ZxDataReqDTO();
		reqDto.setLoanId(loanId);
		reqDto.setProductType(loanInfo.getProductType());
		reqDto.setCustCardNo(loanInfo.getCustCardNo());
		reqDto.setMobileNo(loanInfo.getMobileNo());
		ZxDataRespDTO respDTO = this.consumerExternalApi.getCreditData(reqDto, "001");
		List<Map> respList =(List<Map>)respDTO.getData();
		int mxSize = respList.size();
		for(int n=mxSize-1;n>0;n--) {
			Map respMap = respList.get(n);
			String status = (String)respMap.get("status");
			if(!"100000".equals(status)) {
				continue;
			}
			reqDto.setReqNo((String)respMap.get("request_no"));
			ZxDataRespDTO respDetailDTO = this.consumerExternalApi.getDetailCreditData(reqDto, "001");
			Map detailMap = (Map)respDetailDTO.getData();
			//魔蝎数据中“用户基本信息”姓名与系统中客户信息的姓名进行比对，是否一致，如不一致在风险页签提示；
			String custName = loanInfo.getCustName();
			Map intMxUserBasicInfo = (Map)detailMap.get("intMxUserBasicInfo");
			String mxName = (String)intMxUserBasicInfo.get("name");
			if(custName!=null && !custName.equals(mxName)) {
				riskTip.setDisaccordCount(riskTip.getDisaccordCount() + 1);
				RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
				risk.setCode(1);
				risk.setTitle(loanId + "魔蝎数据中“用户基本信息”姓名与系统中客户信息的姓名不一致");
				risk.getMsg().put("魔蝎数据姓名", mxName);
				risk.getMsg().put("系统中客户信息的姓名", custName);
				riskTip.getTips().add(risk);
			}
			//用户基本信息处，居住地址和工作地址与系统上报的客户信息中“现居地址（市）和公司地址（市）”进行比对，是否一致，如不一致在风险页签提示；
			String curHomeCity = loanInfo.getCurHomeCityName();
			String companyCityName = loanInfo.getCompanyCityName();
			String workAddress = (String)intMxUserBasicInfo.get("workAddress");
			double homeBf = getWord(curHomeCity,workAddress);
			double companyBf = getWord(companyCityName,workAddress);
			if (homeBf<90 || companyBf<90) {
				riskTip.setDisaccordCount(riskTip.getDisaccordCount() + 1);
				RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
				risk.setCode(1);
				if (homeBf < 90) {
					risk.setTitle(loanId + "用户基本信息处，居住地址和工作地址与系统上报的客户信息中现居地址（市）和公司地址（市）不一致");
					risk.getMsg().put("魔蝎数据居住地址", workAddress);
					risk.getMsg().put("系统上现居地址", curHomeCity);
				}
				if (companyBf < 90) {
					risk.getMsg().put("魔蝎数据工作地址", workAddress);
					risk.getMsg().put("系统中公司地址", companyCityName);
				} 
				riskTip.getTips().add(risk);
			}
			//魔蝎数据中与上报联系人无通话次数的，需要在风险页签提示；
			List<Map> intMxCallDetails = (List)detailMap.get("intMxCallDetails");
			//查本地联系人
			List<McamLoanApplyContactInfo> contractInfos = loanApplyContactInfoMapper.selectByLoanId(loanId);
			int size = contractInfos==null?0:contractInfos.size();
			RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
			risk.setTitle("魔蝎数据中与上报联系人无通话次数");
			risk.setCode(1);
			boolean  isHavProblem = false;
			Map<String,Integer> callPhones = callNum(intMxCallDetails);
			for(int i=0;i<size;i++) {
				McamLoanApplyContactInfo contractInfo = contractInfos.get(i);
				String contactName = contractInfo.getContactName();
				contactName = StringUtils.isEmpty(contactName)? "0"+i : contactName;
				String contactMobile = contractInfo.getContactMobile();
				//检查是否在魔蝎中如果不在
				boolean isHav = callCheck(callPhones,contactMobile);
				if(!isHav) {
					risk.getMsg().put("上报联系人"+contactName+"无通话记录", contactMobile);
					isHavProblem = true;
				}
			}
			if(isHavProblem) {
				riskTip.setDisaccordCount(riskTip.getDisaccordCount() + 1);
				riskTip.getTips().add(risk);
			}
			//只拿一条数据检查
			break;
		}
		return riskTip;
	}
	private Map<String,Integer> callNum(List<Map> intMxCallDetails){
		Map<String,Integer> result = new HashMap();
		if(intMxCallDetails!=null&&intMxCallDetails.size()>0) {
			for(Map oneMap:intMxCallDetails) {
				String peerNumber = (String)oneMap.get("peerNumber");
				peerNumber = peerNumber==null?"":peerNumber.trim();
				if(result.containsKey(peerNumber)) {
					Integer num = result.get(peerNumber);
					num++;
					result.put(peerNumber, num);
				}else {
					result.put(peerNumber, 1);
				}
			}
		}
		return result;
	}
	private boolean callCheck(Map<String,Integer> callPhones,String mobile) {
		if(callPhones==null) {
			callPhones = new HashMap();
		}
		return callPhones.containsKey(mobile);
	}
	@Override
	public RiskTipsDataDto queryShzxData(String loanId) throws MarvelsBusinessException {
		// 上海资信数据提示
		RiskTipsDataDto riskTip = new RiskTipsDataDto();
		//查本地流水
		McamLoanApplyInfoDTO loanInfo = loanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
		String mobile = loanInfo.getMobileNo();
		String companyName = loanInfo.getCompanyName();
		ZxDataReqDTO reqDto = new ZxDataReqDTO();
		reqDto.setLoanId(loanId);
		reqDto.setProductType(loanInfo.getProductType());
		reqDto.setCustCardNo(loanInfo.getCustCardNo());
		reqDto.setMobileNo(loanInfo.getMobileNo());
		ZxDataRespDTO respDTO = this.consumerExternalApi.getCreditData(reqDto, "008");
		List<Map> respList =(List<Map>) respDTO.getData();
		if(respList!= null && respList.size()>0) {
			Map respMap = respList.get(0);
			String reqNo = (String)respMap.get("request_no");
			reqDto.setReqNo(reqNo);
			ZxDataRespDTO respDetailDTO = this.consumerExternalApi.getDetailCreditData(reqDto, "008");
			Map dataDetail =(Map) respDetailDTO.getData();
			if(dataDetail!=null) {
				//报告中的手机号码与客户上报手机号码进行比对，是否一致，如不一致在风险页签提示
				Map personalBasic = (Map)dataDetail.get("personalBasic");
				String mobileNo = personalBasic==null?"":(String)personalBasic.get("mobileNo");
				if(mobile!=null && !mobile.equals(mobileNo)) {
					riskTip.setDisaccordCount(riskTip.getDisaccordCount() + 1);
					RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
					risk.setCode(1);
					risk.setTitle(loanId + "报告中的手机号码与客户上报手机号码不一致");
					risk.getMsg().put("报告中的手机号码", mobileNo);
					risk.getMsg().put("客户上报手机号码", mobile);
					riskTip.getTips().add(risk);
				}
				//工作信息中第一条单位名称与客户上报单位名称进行比对，可参考关键字是否一致；如不一致在风险页签提示
				List personalUnit = personalBasic==null?new ArrayList():(List)personalBasic.get("personalUnit");
				if(personalUnit!=null && personalUnit.size()>0 && companyName!=null) {
					Map unit = (Map)personalUnit.get(0);
					String workUnit = (String)unit.get("workUnit");
					if(!companyName.equals(workUnit)) {
						riskTip.setDisaccordCount(riskTip.getDisaccordCount() + 1);
						RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
						risk.setCode(1);
						risk.setTitle(loanId + "工作信息中第一条单位名称与客户上报单位名称不一致");
						risk.getMsg().put("工作信息中第一条单位名称", workUnit);
						risk.getMsg().put("客户上报单位名称", companyName);
						riskTip.getTips().add(risk);
					}
				}
				//如有配偶姓名及联系电话，与客户信息中配偶信息进行比对，是否一致；如不一致在风险页签提示
				Map periouInfo =  personalBasic==null?null:(Map) personalBasic.get("periouInfo");
				if(periouInfo != null) {
//					  `spouse_name` varchar(255) DEFAULT NULL COMMENT '配偶名称',
//					  `spouse_mobile` varchar(30) DEFAULT NULL COMMENT '配偶联系电话',
					String spouseName = loanInfo.getSpouseName();
					String spouseMobile = loanInfo.getSpouseMobile();
//					String private String spouseName;  private String spousePhone;
					String eName =(String) periouInfo.get("spouseName");
					String ePhone =(String) periouInfo.get("spousePhone");
					if(spouseName!=null && !spouseName.equals(eName)) {
						riskTip.setDisaccordCount(riskTip.getDisaccordCount() + 1);
						RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
						risk.setCode(1);
						risk.setTitle(loanId + "配偶姓名不一致");
						risk.getMsg().put("配偶姓名", eName);
						risk.getMsg().put("客户信息中配偶信息", spouseName);
						riskTip.getTips().add(risk);
					}
					if(spouseMobile!=null && !spouseMobile.equals(ePhone)) {
						riskTip.setDisaccordCount(riskTip.getDisaccordCount() + 1);
						RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
						risk.setCode(1);
						risk.setTitle(loanId + "联系电话不一致");
						risk.getMsg().put("联系电话", ePhone);
						risk.getMsg().put("客户信息中配偶信息", spouseMobile);
						riskTip.getTips().add(risk);
					}
				}
				//在【贷款申请信息】中若有显示未通过业务的，超过两次做提示  personalLoanApplyObj.loanApplyList
				Map personalLoanApplyObj = (Map)dataDetail.get("personalLoanApplyObj");
				List loanApplyList = personalLoanApplyObj==null?new ArrayList():(List)personalLoanApplyObj.get("loanApplyList");
				if(loanApplyList!= null && loanApplyList.size() > 2) {
					riskTip.setDisaccordCount(riskTip.getDisaccordCount() + 1);
					RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
					risk.setCode(1);
					risk.setTitle(loanId + "贷款申请信息超过两笔");
					risk.getMsg().put("贷款申请信息笔数",  loanApplyList.size()+"");
					riskTip.getTips().add(risk);
				}
				//未结清贷款笔数超过10次，超过范围做提示；  personalLoanOrderObjInfo.loanOrderInfo.loanCount
				Map personalLoanOrderObjInfo = (Map)dataDetail.get("personalLoanOrderObjInfo");
				Map loanOrderInfo = personalLoanOrderObjInfo==null?new HashMap():(Map)personalLoanOrderObjInfo.get("loanOrderInfo");
				String loanCount = loanOrderInfo==null?"0":(String)loanOrderInfo.get("loanCount");
				Integer count = 0;
				try {
					count = Integer.parseInt(loanCount);
				} catch (NumberFormatException e) {
					
				}
				if( count > 10) {
					riskTip.setDisaccordCount(riskTip.getDisaccordCount() + 1);
					RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
					risk.setCode(1);
					risk.setTitle(loanId + "未结清贷款笔数超过10次");
					risk.getMsg().put("未结清贷款笔数", count+"");
					riskTip.getTips().add(risk);
				}
			}
		}
		
		return riskTip;
	}
	@Override
	public RiskTipsDataDto queryHisData(String loanId) throws MarvelsBusinessException {
		//效验历史数据提示
		RiskTipsDataDto hisData = new RiskTipsDataDto();
		McamLoanApplyInfoDTO loanInfoDto = new McamLoanApplyInfoDTO();
		//查本地流水
		McamLoanApplyInfoDTO loanInfo = loanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
		loanInfoDto.setLoanId(loanId);
		loanInfoDto.setCustCardNo(loanInfo.getCustCardNo());
		//查询本人历史工单
		List<McamLoanApplyInfoDTO> datas = loanApplyInfoDao.selectMcamLoanApplyInfoByCustCardNo(loanInfo);
		if(datas != null) {
			//历史工单中历史业务的单位名称、单电、手机号码与本次申请业务中数据比对，是否一致，如不一致在风险页签提示；
			String companyName = loanInfo.getCompanyName();
			String companyPhone = loanInfo.getCompanyTel();
			String phone = loanInfo.getMobileNo();
			
			for(McamLoanApplyInfoDTO loanDto:datas) {
				String tempCompanyName = loanDto.getCompanyName();
				String tempCompanyPhone = loanDto.getCompanyTel();
				String tempPhone = loanDto.getMobileNo();
				StringBuffer selfMsg = new StringBuffer();
				StringBuffer sbMsg = new StringBuffer();
				boolean isHavProblem = false;
				if(StringUtils.isEmpty(companyName) || !companyName.equals(tempCompanyName)) {//有问题
					sbMsg.append("公司名称[").append(tempCompanyName).append("] ");
					selfMsg.append("公司名称[").append(companyName).append("] ");
					isHavProblem = true;
				}
				if(StringUtils.isEmpty(companyPhone) || !companyPhone.equals(tempCompanyPhone)) {//有问题
					sbMsg.append("单位电话[").append(tempCompanyPhone).append("] ");
					selfMsg.append("单位电话[").append(companyPhone).append("] ");
					isHavProblem = true;
				}
				if(StringUtils.isEmpty(phone) || !phone.equals(tempPhone)) {//有问题
					sbMsg.append("手机号码[").append(tempPhone).append("] ");
					selfMsg.append("手机号码[").append(phone).append("] ");
					isHavProblem = true;
				}
				if(isHavProblem) {
					hisData.setDisaccordCount(hisData.getDisaccordCount()+1);
					RiskTipsDataItem risk = hisData.new RiskTipsDataItem();
					risk.setTitle(loanDto.getLoanId()+"存在信息不一致");
					risk.getMsg().put("本笔信息", selfMsg.toString());
					risk.getMsg().put("历史工单信息", sbMsg.toString());
					risk.setCode(1);
					hisData.getTips().add(risk);
				}
			}
		}
		return hisData;
	}

	public RiskTipsDataDto queryAlipayData(String loanId) throws MarvelsBusinessException {
		//1、支付宝验证是否一致，如不一致在风险页签提示
		RiskTipsDataDto aliData = new RiskTipsDataDto();
		//联系人验证结果
		List<LoanContactCheckDTO> contactChks = taskProcessDao.selectContactChecks(loanId);
		if(contactChks!=null) {
			for(LoanContactCheckDTO  contactChk:contactChks) {
				String mobileNo = contactChk.getMobileNo();
				String contactName = contactChk.getContactName(); 
				//支付宝验证结果：0为无;1为有
				String aliPayCheckResult = contactChk.getAliPayCheckResult(); 
				if(StringUtils.isEmpty(aliPayCheckResult) || aliPayCheckResult.trim().equals("0")) {//有问题
					RiskTipsDataItem risk = aliData.new RiskTipsDataItem();
					risk.setTitle(contactName+mobileNo+"未通过支付宝验证");
					risk.getMsg().put(contactName+mobileNo, "未通过支付宝验证");
					risk.setCode(1);
					aliData.getTips().add(risk);
					aliData.setDisaccordCount(aliData.getDisaccordCount()+1);
				}
			}
		}
		return aliData;
	}
	@Override
	public RiskTipsDataDto queryKNFDData(String loanId) throws MarvelsBusinessException {
		//卡牛房贷还款记录
		RiskTipsDataDto knfdData = new RiskTipsDataDto();
		//查本地流水
		McamLoanApplyInfoDTO loanInfo = loanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
		//数据中户主名与客户信息姓名比对，是否一致；如不一致在风险页签提示
		ZxDataReqDTO reqDto = new ZxDataReqDTO();
		reqDto.setLoanId(loanId);
		ZxDataRespDTO respDTO = this.consumerExternalApi.getCreditData(reqDto, "012");
		List<Map<String,Object>> datas = (List)respDTO.getData();
		int size = datas == null?0:datas.size();
		String userNm = loanInfo.getCustName();
		Set<String> cardNos = new HashSet();
		for(int i=0;i<size;i++) {
			Map<String,Object> oneData = datas.get(i);
			String cardNo = (String)oneData.get("bank_card");
			String userName = (String)oneData.get("userName");
			if(!cardNos.contains(cardNo) && userName!=null && !userName.equals(userNm)) {//有问题
				RiskTipsDataItem risk = knfdData.new RiskTipsDataItem();
				risk.setCode(1);
				risk.setTitle(cardNo+"名字"+userName+"不一致");
				risk.getMsg().put("客户信息姓名",userNm);
				risk.getMsg().put("卡牛数据中户主名",userName);
				knfdData.getTips().add(risk);
				knfdData.setDisaccordCount(knfdData.getDisaccordCount()+1);
			}
			
		}
		return knfdData;
	}
	@Override
	public RiskTipsDataDto queryGjj51Data(String loanId) throws MarvelsBusinessException {
		//51公积金数据
		RiskTipsDataDto riskTip = new RiskTipsDataDto();
		//查本地流水
		McamLoanApplyInfoDTO loanInfo = loanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
		//1、基本信息中，缴交单位名称与客申请单位比对，抓取关键字，是否一致；如不一致在风险页签提示
		ZxDataReqDTO reqDto = new ZxDataReqDTO();
		reqDto.setLoanId(loanId);
		ZxDataRespDTO respDTO = this.consumerExternalApi.getCreditData(reqDto, "009");
		List<Map<String, Object>> gjj51List = (List<Map<String, Object>>) respDTO.getData();
		if(gjj51List!=null && gjj51List.size()>0) {
			Map<String, Object> gjj51 = gjj51List.get(0);
			String reqNo = (String)gjj51.get("orderId");
			ZxDataReqDTO detailDTO = new ZxDataReqDTO();
			detailDTO.setReqNo(reqNo);
			ZxDataRespDTO respDetailDTO = this.consumerExternalApi.getDetailCreditData(detailDTO, "009");
			
			Map<String, Object> respDetail = (Map<String,Object>) respDetailDTO.getData();
			Map<String,Object> account = (Map<String, Object>) respDetail.get("account");
			if(account != null) {
				String gjjCompanyName = (String)account.get("company");
				String companyname = loanInfo.getCompanyName();
				double bf = getWord(gjjCompanyName,companyname);
				if(bf<90) {
					riskTip.setDisaccordCount(riskTip.getDisaccordCount()+1);
					RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
					risk.setCode(1);
					risk.setTitle(loanId+"公司名称与缴纳公积金公司不一致");
					risk.getMsg().put("贷款信息中公司",companyname);
					risk.getMsg().put("缴纳公积金公司",gjjCompanyName);
					riskTip.getTips().add(risk);
				}
			}
		}
		return riskTip;
	}
	@Override
	public RiskTipsDataDto queryKnxssData(String loanId) throws MarvelsBusinessException {
		// 卡牛洗刷刷魔蝎
		RiskTipsDataDto riskTip = new RiskTipsDataDto();
		//查本地流水
		McamLoanApplyInfoDTO loanInfo = loanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
		String custName = loanInfo.getCustName();
//		1、魔蝎数据中抓取姓名与系统客户信息姓名比对，是否一致；如不一致在风险页签提示
		ZxDataReqDTO reqDto = new ZxDataReqDTO();
		reqDto.setLoanId(loanId);
//		{kn_loan_id=N180518426483002162, request_no=18051810360012600, created_date=1526610327000}
		ZxDataRespDTO respDTO = this.consumerExternalApi.getCreditData(reqDto, "013"); 
		List dataList = respDTO == null?new ArrayList():(List)respDTO.getData();
		Object value = dataList.get(0);
		if(value != null && value instanceof Map) {
			Map data= (Map)value; 
			String crawlName = (String)data.get("crawl_name");
			if(custName!=null && !custName.equals(crawlName)) {
				riskTip.setDisaccordCount(riskTip.getDisaccordCount()+1);
				RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
				risk.setCode(1);
				risk.setTitle(loanId+"魔蝎数据中抓取姓名与系统客户信息姓名不一致");
				risk.getMsg().put("魔蝎数据中抓取姓名",crawlName);
				risk.getMsg().put("系统客户信息姓名",custName);
				riskTip.getTips().add(risk);
			}
		}
		return riskTip;
	}

	@Override
	public RiskTipsDataDto queryKnData(String loanId) throws MarvelsBusinessException {
		// 卡牛信用卡数据
		RiskTipsDataDto riskTip = new RiskTipsDataDto();
		// 1、授信数据中，卡片剩余可用额度为负数，需在风险页签提示；
		ZxDataReqDTO reqDto = new ZxDataReqDTO();
		reqDto.setLoanId(loanId);
		ZxDataRespDTO respDTO = this.consumerExternalApi.getCreditData(reqDto, "011");
		Object value = respDTO.getData();
		if(value !=null && value instanceof List) {
			List datas = (List)value;
			int size = datas.size();
			for(int i=0;i<size;i++) {
				Map one = (Map)datas.get(i);
				String requestNo=(String)one.get("request_no");
				reqDto.setReqNo(requestNo);
				ZxDataRespDTO respData = consumerExternalApi.getDetailCreditData(reqDto, "011");
				Object dataObj = respData.getData();
				if(dataObj!=null && dataObj instanceof Map) {
					Map dataMap = (Map)dataObj;
					Map oneCredit = (Map)dataMap.get("creditmap");
					if (oneCredit!=null) {
						String limitAvail = (String) oneCredit.get("limit_avail");
						BigDecimal avail = BigDecimal.ZERO;
						try {
							avail = new BigDecimal(limitAvail);
						} catch (Exception e) {
							avail = new BigDecimal(-1);
						}
						if (BigDecimal.ZERO.compareTo(avail) > 0) {
							riskTip.setDisaccordCount(riskTip.getDisaccordCount() + 1);
							RiskTipsDataItem risk = riskTip.new RiskTipsDataItem();
							risk.setCode(1);
							risk.setTitle(loanId + "卡片剩余可用额度为负数");
							risk.getMsg().put("卡片剩余可用额度", limitAvail);
							riskTip.getTips().add(risk);
						} 
					}
				}
			}
		}
		return riskTip;
	}
	
	private double getWord(String word1,String word2) {
		if(word2==null || word1==null) {
			return 0;
		}
		int word1Size= word1.length();
		int word2Size = word2.length();
		int count = 0;
		for(int i=0;i<word1Size;i++) {
			for(int j=0;j<word2Size;j++) {
				String char1 = word1.charAt(i)+"";
				String char2 = word2.charAt(j) + "";
				if(char1.equals(char2)) {
					count++;
					break;
				}
			}
		}
		double bf1 = count/word1Size * 100;
		double bf2 = count/word2Size * 100;
		double bf = bf1>bf2?bf2:bf1;
		return bf;
	}
	
	
	/**
	 * 
	* @Title: queryAutoDecisionData 
	* @Description: 获取自动审核数据 
	* @param loanId
	* @return
	* @throws MarvelsBusinessException       
	* @author 李瑶
	* @date 2018年10月9日 上午9:37:00
	 */
	@Override
	public McamAutoDecisionWithBLOBs queryAutoDecisionData(String loanId, String status) throws MarvelsBusinessException {
		McamAutoDecisionWithBLOBs mcamAutoDecisionWithBLOBs = mcamAutoDecisionMapper.selectByLoanId(loanId);
		if(mcamAutoDecisionWithBLOBs != null){
			List<McamAutoDecisionRecord> decisionRecordList = mcamAutoDecisionRecordMapper.selectByDecisionId(mcamAutoDecisionWithBLOBs.getId(), status);
			mcamAutoDecisionWithBLOBs.setMcamAutoDecisionRecordList(decisionRecordList);
		}
		return mcamAutoDecisionWithBLOBs;
	}
	
	/**
	 * 
	* @Title: queryAntiFraud 
	* @Description:拉取反欺诈数据  
	* @param loanId
	* @return
	* @throws MarvelsBusinessException       
	* @author 李瑶
	* @date 2018年10月22日 上午11:30:10
	 */
	public List queryAntiFraud(String loanId) throws MarvelsBusinessException {
		String loanIdHis =  thirdCreditReportService.getThirdLoanId(loanId);

		McamLoanApplyInfoDTO loanInfo = loanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
		String userCde = loanInfo.getCustCode();

		Map<String, Object> map = creditReportService.getRiskData(loanId,loanIdHis, userCde);
		List  list = new ArrayList<>();
		if(map != null){
			for(String key : map.keySet()){
				Map<String, Object> oneData = new HashMap<>();
				if("eqcIdNumFin".equals(key)){
					oneData.put("name", "金融行业中，匹配设备关联身份证号个数");
				}else if("eqcCellNumFin".equals(key)){
					oneData.put("name", "金融行业中，匹配设备关联手机号个数");
				}else if("eqcIdGidNumFin".equals(key)){
					oneData.put("name", "金融行业中，匹配设备关联身份证号对应关联的设备数量");
				}else if("eqcCellGidNumFin".equals(key)){
					oneData.put("name", "金融行业中，匹配设备关联手机号对应关联的设备数量");
				}else if("appCt".equals(key)){
					oneData.put("name", "设备中贷款app个数");
				}else if("root".equals(key)){
					oneData.put("name", "设备是否越狱");
				}else if("maliceNum".equals(key)){
					oneData.put("name", "恶意app安装个数");
				}else if("isRunMalice".equals(key)){
					oneData.put("name", "后台是否运行恶意app");
				}else{
					continue;
				}
				oneData.put("approveRule", map.get(key));
				list.add(oneData);
			}
		}
		return list;
	}
}
